<?php

	if($_POST['act']=="add")
	{
		include_once("$config[webroot]/module/activity/includes/plugin_activity_class.php");
		$activity=new activity();
		$flag=$activity->save_activity_product();
		echo "<script>window.parent.main.location.reload();</script>";
		echo "<script>window.parent.DialogManager.close('activity');</script>";
		die;
	}
	
	//要排除正在活动中的商品
	$now_time = strtotime(date("Y-m-d H:i:s"));
	$sql = "select a.product_id from ".ACTIVITYPRODUCT." as a left join ".ACTIVITY." as b on b.id=a.activity_id where b.status>0 and b.end_time>'".$now_time."'";
	$product_in_activity = array();
	$db->query($sql);
	$product_in_activity = $db->getRows();
	$except_ids = "";
	foreach ($product_in_activity as $val)
	{
		$except_ids .= $val['product_id'].",";
	}
	
	$id=$_GET['id']?$_GET['id']:NULL;
	$product_id=$_GET['product_id']?$_GET['product_id']:NULL;
	$key=$_GET['key']?$_GET['key']:NULL;
	$catid=$_GET['catid']?$_GET['catid']:NULL;
	$code=$_GET['code']?$_GET['code']:NULL;
	$tags=$_GET['tags']?$_GET['tags']:NULL;
	
	if(!empty($key))
	    $scl.=" and ( pname like '%$key%' )";
	if(!empty($catid))
	    $scl.=" and LOCATE($catid,catid)=1";
	if(!empty($tags))
	    $scl.=" and (tags like '$tags%' or tags like '%,$tags%') ";
	if(!empty($code))
	    $scl.=" and code = '$code'";
	if(!empty($product_id))
		$scl.=" and id in($product_id)";
	if($except_ids!=""){
		$scl.= " and id not in (".rtrim($except_ids,',').")";
	}
    if($_GET['brand'] && $_GET['brandid']){
	    $scl.=" and brandid = '$_GET[brandid]'";
	}else{
	    $scl.=" and brand like '".addslashes($_GET[brand])."%'";
	}
	if ($_GET['page_size']){
	    $page_size=$_GET['page_size'];
	}else{
	    $_GET['page_size']=$page_size=20;
	}
	
	$sql="SELECT id,catid,tags,brand,pic,pname,price,show_price,market_price,code,tax_rate FROM ".PRO." where status>0 $scl order by id desc";
	//====================
	include_once("../includes/page_utf_class.php");
	$page = new Page;
	$page->listRows=$page_size;
	if (!$page->__get('totalRows')){
	    $db->query($sql);
	    $totalRows = $page->totalRows = $db->num_rows();
	}else{
	    $totalRows = $_GET['totalRows'];
	}
	$sql .= "  limit ".$page->firstRow.",".$page->listRows;
	$de['page'] = $page->prompt();
	//=====================
	$db->query($sql);
	$de['list']=$db->getRows();
	//$tpl->assign("de",$de);

	if(!empty($catid) and $de['list'])
	{
	    $max=$catid.'99';
	    $min=$catid.'00';
	    $ss=" and catid<=$max and catid>=$min ";
	}
	elseif(!empty($catid) and !$de['list'])
	{
	    $catid=substr($catid,0,-2);
	    $max=$catid.'99';
	    $min=$catid.'00';
	    $ss=" and catid<=$max and catid>=$min ";
	}
	else
	    $ss=" and catid<9999 ";
	
	$sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
	$db->query($sql);
	$re=$db->getRows();
	if(!$re)
	{
	    $catid=substr($catid,0,-2);
	    $max=$catid.'99';
	    $min=$catid.'00';
	    $ss=" and catid<=$max and catid>=$min ";
	    $sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
	    $db->query($sql);
	    $re=$db->getRows();
	}
	$tpl->assign("re",$re);
	
	include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
	//================================================================
	$pro=new pro();
	$de['all_tags']=$pro->get_tags();
	
	if ($_GET['tags']){
	    	
	    $de['tags']=array($_GET['tags']);
	    	
	    	
	}
	
	//所属分类，产品标签
	$catids = "";
	$tagids = "";
	$catTmp = array();
	$tagTmp = array();
	foreach ($de['list'] as $dk=>$dv)
	{
	    $catids .= $dv['catid'].",";
	    $tagids .= $dv['tags'].",";
	}
	$catids = rtrim($catids,",");
	if($catids!=""){
	    $sql="SELECT cat,catid FROM ".PCAT." WHERE catid IN (".$catids.")";
	    $db->query($sql);
	    $catTmp=$db->getRows();
	}
	$catArr = array();
	foreach ($catTmp as $cav){
	    $catArr[$cav['catid']] = $cav['cat'];
	}
	$tagids = rtrim($tagids,",");
	if($tagids!=""){
	    $sql="SELECT id,tags FROM ".TAG." WHERE id IN (".$tagids.")";
	    $db->query($sql);
	    $tagTmp=$db->getRows();
	}
	$tagArr = array();
	foreach ($tagTmp as $tav)
	{
	    $tagArr[$tav['id']] = $tav['tags'];
	}
	
	
	foreach ($de['list'] as $dk=>$dv)
	{
	    $de['list'][$dk]['catname'] = $catArr[$dv['catid']];
	    $tagsTmp = explode(",", $dv['tags']);
	    $tagStr = "";
	    foreach ($tagsTmp as $ttv)
	    {
	        $tagStr .= $tagArr[$ttv].",";
	    }
	    $de['list'][$dk]['tagnames'] = rtrim($tagStr,",");
	}
	
	$tpl->assign("de",$de);
	$tpl->assign("countpro",$totalRows);
	$condTemp = $_GET;
	unset($condTemp['m']);
	unset($condTemp['s']);
	$cond = "";
	if(count($condTemp)){
	    $cond = "&".http_build_query($condTemp);
	}
	$tpl->assign("cond",$cond);
	$tpl->assign("config",$config);
	$tpl->display("activity_product.htm");
	
?>